package com.dwjm.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dwjm.entity.Banner;
import com.dwjm.service.BannerService;

/**
 * banner Controller层
 * 
 * @date 2016年12月26日
 * @auther luans
 */
@RequestMapping("banctr")
@Controller
public class BannerController {

	@Resource
	private BannerService bannerServiceImpl;

	/**
	 * 去banner列表页面：分页查询
	 * 
	 * @param Banner
	 * @return ModelAndView
	 * @date 2016年12月26日
	 * @auther luans
	 */
	@RequestMapping("banlist")
	public ModelAndView queryListByPage(Banner banner) {
		ModelAndView mv = new ModelAndView();
		if (banner.getPageInfo().getPageSize() < 10) {// 每页显示条数
			banner.getPageInfo().setPageSize(10);
		}
		List<Banner> list = bannerServiceImpl.queryListByPage(banner);

		mv.setViewName("/manager/banner/bannerList");

		mv.addObject("list", list);
		mv.addObject("banner", banner);

		return mv;
	}

	/**
	 * 去banner操作页面：添加、编辑、查看
	 * 
	 * @param Banner
	 * @return ModelAndView
	 * @date 2016年12月26日
	 * @auther luans
	 */
	@RequestMapping("tobanmanage")
	public ModelAndView tobanManage(Banner banner) {
		ModelAndView mv = new ModelAndView();
		if (banner.getMarkManage() == 1) {
			mv.addObject("news", banner);
			mv.setViewName("/manager/banner/bannerManage");
		} else if (banner.getMarkManage() == 2) {

			Banner b = bannerServiceImpl.selectByPrimaryKey(banner.getBanId());
			b.setCondition(banner.getCondition());
			b.setMarkManage(banner.getMarkManage());
			b.setPageInfo(banner.getPageInfo());
			mv.addObject("banner", b);
			mv.setViewName("/manager/banner/bannerManage");
			return mv;
		} else if (banner.getMarkManage() == 3) {

			Banner b = bannerServiceImpl.selectByPrimaryKey(banner.getBanId());
			b.setCondition(banner.getCondition());
			b.setMarkManage(banner.getMarkManage());
			b.setPageInfo(banner.getPageInfo());
			mv.addObject("banner", b);
			mv.setViewName("/manager/banner/bannerSel");
			return mv;
		}

		return mv;

	}

	/**
	 * 文案操作：查看、编辑、删除、展示、不展示
	 * 
	 * @param Banner
	 * @return ModelAndView
	 * @date 2016年12月24日
	 * @auther luans
	 */
	@RequestMapping("banmanage")
	public ModelAndView banManage(Banner banner) {

		if (banner.getMarkManage() == 1) {
			bannerServiceImpl.insertSelective(banner);
		} else if (banner.getMarkManage() == 2) {
			bannerServiceImpl.updateByPrimaryKey(banner);
		} else if (banner.getMarkManage() == 4) {
			Banner b = new Banner();
			b.setBanId(banner.getBanId());
			b.setBanStatus(3);// //状态：1展示中；2不展示；3删除；4删除；5不展示；6展示
			bannerServiceImpl.updateByPrimaryKey(b);
		} else if (banner.getMarkManage() == 5 || banner.getMarkManage() == 6) {
			Banner b = new Banner();
			b.setBanId(banner.getBanId());
			if(banner.getMarkManage() == 5){
				b.setBanStatus(2);// //状态：1展示中；2不展示；3删除
				b.setBanOrder(100);//排序改为默认值
			}else if(banner.getMarkManage() == 6){
				b.setBanStatus(1);// //状态：1展示中；2不展示；3删除
				b.setBanOrder(9);//设置为最多排序位置
			}
			bannerServiceImpl.updateByPrimaryKey(b);
		}

		return queryListByPage(banner);
	}

}
